

package io.lkt.service;

import io.lkt.common.utils.DateUtil;
import io.lkt.commons.dynamic.datasource.annotation.DataSource;
import io.lkt.modules.sys.dao.BOrderInfoDao;
import io.lkt.modules.sys.dao.BUserInfoDao;
import io.lkt.modules.sys.dao.SysUserDao;
import io.lkt.modules.sys.dao.SysUserGzhDao;
import io.lkt.modules.sys.entity.*;
import io.lkt.modules.sys.service.BUserInfoService;
import io.lkt.modules.sys.service.BUserWithdrawLogService;
import io.lkt.modules.sys.shiro.WeixinService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner;
import io.lkt.commons.dynamic.datasource.annotation.DataSource;
import io.lkt.modules.sys.dao.SysUserDao;
import io.lkt.modules.sys.entity.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 测试多数据源
 *
 * @author xss dustwas@163.com
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Service
//@DataSource("slave1")
public class DynamicDataSourceTestService {
    @Autowired
    private SysUserDao sysUserDao;

    @Autowired
    private BOrderInfoDao bOrderInfoDao;

    @Autowired
    private BUserInfoDao bUserInfoDao;

    @Autowired
    private BUserInfoService bUserInfoService;

    @Autowired
    private SysUserGzhDao sysUserGzhDao;

    @Autowired
    private BUserWithdrawLogService bUserWithdrawLogService;

    @Transactional
    public void updateUser(Long id){
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("13500000000");
        sysUserDao.updateById(user);
    }

    @Transactional
    @DataSource("slave1")
    public void updateUserBySlave1(Long id){
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("13500000001");
        sysUserDao.updateById(user);
    }

    @DataSource("slave2")
    @Transactional
    public void updateUserBySlave2(Long id){
        SysUserEntity user = new SysUserEntity();
        user.setUserId(id);
        user.setMobile("13500000002");
        sysUserDao.updateById(user);

        //测试事物
        int i = 1/0;
    }

    @Test
    public void test() {
        BUserInfoEntity userInfo = bUserInfoService.getById(179668);
        BOrderInfoEntity order = bOrderInfoDao.getInfo("201912191234026641703834");
        if (userInfo != null && userInfo.getOpenId() != null && order != null) {

            SysGzhEntity sysGzh = sysUserGzhDao.getGzhByCode(userInfo.getGzhCode());

            // 发送模板消息接口
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    // 接收者openid
                    .toUser(userInfo.getOpenId())
                    // 模板id
                    .templateId(sysGzh.getRefundTempId())
                    .build();
            String firstMsg = sysGzh.getGzhName() + "提醒您，您有一笔退款成功，请留意。\n\n";
            String remarkMsg = "如还有疑问，请回复【客服】咨询服务。\n\n";

            // 添加模板数据
            templateMessage.addData(new WxMpTemplateData("first", firstMsg, "#FF00FF"))
                    .addData(new WxMpTemplateData("keyword1", order.getOrderId(), "#A9A9A9"))
                    .addData(new WxMpTemplateData("keyword2",  order.getTotalAmount()+ "元", "#A9A9A9"))
                    .addData(new WxMpTemplateData("remark", remarkMsg, "#0000FF"));
            String msgId = null;
            try {
                WxMpServiceImpl wxMpService = WeixinService.getWeixinService(sysGzh);
                // 发送模板消息
                msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (WxErrorException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test2() {
        BUserInfoEntity bUserInfo = bUserInfoService.getById(179668);
        SysGzhEntity sysGzh= sysUserGzhDao.getGzhByCode(bUserInfo.getGzhCode());

        BUserWithdrawLogEntity bUserWithdrawLog=bUserWithdrawLogService.getById(4);

        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                // 接收者openid
                .toUser(bUserInfo.getOpenId())
                // 模板id
                .templateId(sysGzh.getWithdrawTempId())
                .build();
        String firstMsg =sysGzh.getGzhName() +"您申请的提现金额已到帐。\n\n";
        String remarkMsg = "感谢你的使用。\n\n成为代理，获得更高收益，回复【客服】咨询开通。\n\n";

        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", firstMsg, "#FF00FF"))
                .addData(new WxMpTemplateData("keyword1", "银行卡转帐", "#A9A9A9"))
                .addData(new WxMpTemplateData("keyword2",  bUserWithdrawLog.getAmount().toString(), "#A9A9A9"))
                .addData(new WxMpTemplateData("keyword3", DateUtil.getFormatDate("",bUserWithdrawLog.getCreateTime()) , "#A9A9A9"))
                .addData(new WxMpTemplateData("remark", remarkMsg, "#0000FF"));
        String msgId = null;
        try {
            WxMpServiceImpl wxMpService= WeixinService.getWeixinService(sysGzh);
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
}